using System;
using System.Collections.Generic;
using System.Text;

namespace Yozeev.Help
{
    public class TimeHelp
    {
        /// <summary>

        /// 计算某年第一周的天数
        /// </summary>
        /// <param name="dt">某年中的一个时间</param>
        /// <returns></returns>
        public static int DaysInFirstweekInYear(DateTime dt)
        {
            DateTime FirstDate = Convert.ToDateTime(string.Format("{0}-1-1", dt.Year));
            int DayOfWeekInYear = DayOfWeek(FirstDate);
            int DaysInFirstWeek = 8 - DayOfWeekInYear;
            return DaysInFirstWeek;
        }

        /// <summary>
        /// 一年中的第几周
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static int WeekOfYear(DateTime dt)
        {
            int days = DaysInFirstweekInYear(dt);
            int dayofyear = dt.DayOfYear;
            if (dayofyear < days) return 1;
            else
            {
                int week2 = (int)Math.Ceiling((double)(dayofyear - days) / (double)7);
                return week2 + 1;
            }
        }
        /// <summary>
        /// 传入某一天，输出这一周的起始日期
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static DateTime[] DatesIncludeDay(DateTime dt)
        {
            DateTime[] dts = new DateTime[2];
            int dayofweek = DayOfWeek(dt);
            dts[0] = dt.AddDays(1 - dayofweek);
            dts[1] = dt.AddDays(7 - dayofweek);
            return dts;
        }
        /// <summary>
        /// 计算星期几，转换为数字
        /// </summary>
        /// <param name="dt">某天的日期</param>
        /// <returns></returns>
        public static int DayOfWeek(DateTime dt)
        {
            string strDayOfWeek = dt.DayOfWeek.ToString().ToLower();
            int intDayOfWeek = 0;
            switch (strDayOfWeek)
            {
                case "monday":
                    intDayOfWeek = 1;
                    break;
                case "tuesday":
                    intDayOfWeek = 2;
                    break;
                case "wednesday":
                    intDayOfWeek = 3;
                    break;
                case "thursday":
                    intDayOfWeek = 4;
                    break;
                case "friday":
                    intDayOfWeek = 5;
                    break;
                case "saturday":
                    intDayOfWeek = 6;
                    break;
                case "sunday":
                    intDayOfWeek = 7;
                    break;
            }
            return intDayOfWeek;

        }
    }
}
